package Json2RDF.Surface2RDF.SurfaceWriter;

import Json2RDF.Json2Java.OntologyClasses.ToroidalSurface;
import Json2RDF.Surface2RDF.IndexTable.Face2ResourceIndexTable;
import Json2RDF.Surface2RDF.IndexTable.FaceIndexTable;
import RDFManager.PropertySet;
import RDFManager.RDFHead;
import RDFManager.RDFReader;
import RDFManager.ResourceSet;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;

import java.util.List;
import java.util.stream.IntStream;

public class ToroidalSurfaceWriter {
  public static void writeList(List<ToroidalSurface> toroidalSurfaceList) {

    IntStream.range(0, toroidalSurfaceList.size()).forEach(i -> {
      write(toroidalSurfaceList.get(i), "toroidalSurface" + (i + 1));
      Face2ResourceIndexTable.faceResourceIdxTbl.computeIfAbsent(toroidalSurfaceList.get(i), v -> RDFReader.model.getResource(RDFHead.namespace + "toroidalSurface" + (i + 1)));
      FaceIndexTable.faceIdxTbl.computeIfAbsent(toroidalSurfaceList.get(i).getId(), v -> toroidalSurfaceList.get(i));
    });
  }


  private static void write(ToroidalSurface toroidalSurface, String localName) {
    Resource inst = RDFReader.model.createResource(RDFHead.namespace + localName);
    RDFReader.model.add(inst, RDF.type, ResourceSet.Face);
    RDFReader.model.add(inst, RDF.type, ResourceSet.toroidalSurface);
    inst.addLiteral(PropertySet.hasMajorRadius, toroidalSurface.getMajorRadius());
    inst.addLiteral(PropertySet.hasMinorRadius, toroidalSurface.getMinorRadius());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasCentralPoint", toroidalSurface.getCentralPoint());
    RDFTripleArrayWriter.writeTripleArray(inst, "hasAxisVector", toroidalSurface.getAxisVector());
  }
}
